<?php
/*
Template Name: comparison Result
Auther: Naseer Ahnad
Created: 10-03-2012
*/
?>
<?php 
global $wpdb;


function get_category_id($cat_name){
	$term = get_term_by('name', $cat_name, 'category');
	return $term->term_id;
}

function get_post_meta_all($post_id){
    global $wpdb;
    $data   =   array();
    $wpdb->query("
        SELECT `meta_key`, `meta_value`
        FROM $wpdb->postmeta
        WHERE `post_id` = $post_id
    ");
    foreach($wpdb->last_result as $k => $v){
        $data[$v->meta_key] =   $v->meta_value;
    };
    return $data;
}

function isYes($val){
	$val=strtolower(trim($val));
	if($val=="yes" or $val=="y") return true;
	return false;
}

function getVenuInfo($query, $venueID){
		
//	echo $venueID;
	$data=array();
	$vInfo= get_post_meta_all($venueID);
	
//	print "<pre>";
//	print_r($vInfo);
//	print "</pre>";

	///////// Place of Ceremony
	if(isYes($vInfo['placeOfCeremony']) && $query['placeOfCeremony']=='Yes'){
		if(isInRange($vInfo['placeOfCeremonyRange'], $query['guestsRoom'])==true){
			$data['placeOfCeremony']=$query['guestsRoom'] * $vInfo['placeOfCeremonyRate'];
		}
		else
			$data['placeOfCeremony']=0;	
	}
	else
		$data['placeOfCeremony']=0;
	
	///////// dayReception
	if(isYes($vInfo['dayReception']) && $query['dayReception']=='Yes'){
		if(isInRange($vInfo['dayReceptionRange'], $query['guestsRoom'])==true)
			$data['dayReception']=$query['guestsRoom'] * $vInfo['dayReceptionRate'];
		else
			$data['dayReception']=0;	
	}
	else
		$data['dayReception']=0;


	///////// eveningReception
	if(isYes($vInfo['eveningReception']) && $query['eveningReception']=='Yes'){
		if(isInRange($vInfo['eveningReceptionRange'], $query['guestsRoom'])==true)
			$data['eveningReception']=$query['guestsRoom'] * $vInfo['eveningReceptionRate'];
		else
			$data['eveningReception']=0;	
	}
	else
		$data['eveningReception']=0;


	///////// exclusiveAvailable


	///////// accommodation
	if(isYes($vInfo['accommodation']) && $query['accommodation']=='Yes'){
		if(isInRange($vInfo['accommodatioRange'], $query['guestsRoom'])==true){
			$data['accommodation']=$query['guestsRoom'] * $vInfo['accommodationRate'];
		}
		else
			$data['accommodation']=0;	
	}
	else
		$data['accommodation']=0;

	///////// arrivalDrinks
	if($vInfo['arrivalDrinks']!="No" && $query['arrivalDrinks']!='No'){
		if(isInRange($vInfo['arrivalDrinksRange'], $query['guests'])==true)
			$data['arrivalDrinks']=$query['guests'] * $vInfo['arrivalDrinksRate'];
		else
			$data['arrivalDrinks']=0;	
	}
	else
		$data['arrivalDrinks']=0;


	///////// adultBreakfast
	if(isYes($vInfo['adultBreakfast']) && $query['adultBreakfast']=='Yes'){
		if(isInRange($vInfo['adultBreakfastRange'], $query['guestsBreakfast'])==true)
			$data['adultBreakfast']=$query['guestsBreakfast'] * $vInfo['adultBreakfastRate'];
		else
			$data['adultBreakfast']=0;	
	}
	else
		$data['adultBreakfast']=0;

	///////// child1Breakfast
	if(isYes($vInfo['child1Breakfast']) && $query['child1Breakfast']=='Yes'){
		if(isInRange($vInfo['child1BreakfastRange'], $query['guestsBreakfast'])==true)
			$data['child1Breakfast']=$query['guestsBreakfast'] * $vInfo['child1BreakfastRate'];
		else
			$data['child1Breakfast']=0;	
	}
	else
		$data['child1Breakfast']=0;

	///////// child2Breakfast
	if(isYes($vInfo['child2Breakfast']) && $query['child2Breakfast']=='Yes'){
		if(isInRange($vInfo['child2BreakfastRange'], $query['guestsBreakfast'])==true)
			$data['child2Breakfast']=$query['guestsBreakfast'] * $vInfo['child2BreakfastRate'];
		else
			$data['child2Breakfast']=0;	
	}
	else
		$data['child2Breakfast']=0;


	///////// toastDrink
	if($vInfo['toastDrink']!="No" and $query['toastDrink']!='No'){
		if(isInRange($vInfo['toastDrinkRange'], $query['guestsBreakfast'])==true)
			$data['toastDrink']=$query['guestsBreakfast'] * $vInfo['toastDrinkRate'];
		else
			$data['toastDrink']=0;	
	}
	else
		$data['toastDrink']=0;


	///////// adultBuffet
	if(isYes($vInfo['adultBuffet']) && $query['adultBuffet']=='Yes'){
		if(isInRange($vInfo['adultBuffetRange'], $query['guestsBuffet'])==true)
			$data['adultBuffet']=$query['guestsBuffet'] * $vInfo['adultBuffetRate'];
		else
			$data['adultBuffet']=0;	
	}
	else
		$data['adultBuffet']=0;

	///////// child1Buffet
	if(isYes($vInfo['child1Buffet']) && $query['child1Buffet']=='Yes'){
		if(isInRange($vInfo['child1BuffetRange'], $query['guestsBuffet'])==true)
			$data['child1Buffet']=$query['guestsBuffet'] * $vInfo['child1BuffetRate'];
		else
			$data['child1Buffet']=0;	
	}
	else
		$data['child1Buffet']=0;

	///////// child2Buffet
	if(isYes($vInfo['child2Buffet']) && $query['child2Buffet']=='Yes'){
		if(isInRange($vInfo['child2BuffetRange'], $query['guestsBuffet'])==true)
			$data['child2Buffet']=$query['guestsBuffet'] * $vInfo['child2BuffetRate'];
		else
			$data['child2Buffet']=0;	
	}
	else
		$data['child2Buffet']=0;



	
	///// discount and totals
	$total=array_sum($data);
	$data['discount']=intval($vInfo['discount']);
	$data['total']=$total;
	$data['gtotal']=$total-($total*$data['discount']/100);
	
	$data['rating']=$vInfo['rating'];
	return $data;
}

function isInRange($range, $value){
	$range = trim($range);
	$value=floatval(trim($value));
	
	if($range=="") return true;
	
	if(substr($range,-1,1)=="+"){
		$digit=number_format($range);
		if($value<=$digit) return true; else return false;
	}
	
	$digits = explode("-", $range);
	$digits[0]=intval($digits[0]);
	if(count($digits)>1) $digits[1]=intval($digits[1]);
	
	if(count($digits)==1)
		if($value<=$digits[1]) return true; else return false;
	else
		if($value>=$digits[0] and $value<=$digits[1]) return true; else return false;
	
	return false;
}

/////// get all parents of specifc area
$parentCatList = get_category_parents($_POST['area'],false,',');
if(is_string($parentCatList)) $parentCatListArray = split(",",$parentCatList); else $parentCatListArray=array();
if(count($parentCatListArray)>0) array_shift($parentCatListArray);		///////// removing top most category i-e Countries

$areaID=array();
foreach($parentCatListArray as $pCat){
	if(trim($pCat)!="") $areaID[]=get_category_id(trim($pCat));
}

$typeID = array(6, $_POST['venue']);

query_posts(array('category__and'=> $typeID, 'category__in'=> $areaID));
$venueIDs = array();

if(have_posts()){
	while ( have_posts() ) : the_post();
		$venueIDs[]=array('ID'=>get_the_ID(), "Name"=>get_the_title());
	endwhile;	
}

///////////// Geting Data
$venuesRates=array();

foreach($venueIDs as $venue){
	$venuesRates[]=getVenuInfo($_POST, $venue['ID']);
}

//ob_end_clean();

//print "<pre>";
//print_r($venuesRates);
//print "</pre>";
//exit;

get_header(); 
?>
<script type="text/javascript">
function updateRating(id, value){
	postID = id.substr(5, id.length-5);
	jQuery.post("http://armchairweddingguru.co.uk/comparison-result/",{postID:postID, value:value, action:"updateRate"}, function(data){
		alert(data);
	});
}
</script>
<div class="main-content-inner global-width">
    
    <section class="contents full-width">
        <h1>Wedding Venue Comparision</h1>

        <table>
        <tr>
            <th width="220"></th>
            <th width="70">Item</th>
            <th width="50">Pr @</th>
            <?php foreach($venueIDs as $venue): ?>
            <th width="100"><a href="<?php echo  get_permalink( $venue['ID'] ); ?>"><?=$venue['Name']?></a></th>
            <?php endforeach; ?>
        </tr>

<!--        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<?php $i=0; foreach($venueIDs as $venue): ?>
            	<select onchange="updateRating(this.id, this.value)" id="rate-<?php echo $venue['ID'] ?>">
                <option value="5" <?php if(trim($venuesRates[$i]['rating'])=="5") echo 'selected="selected"' ?>>Excellent</option>
                <option value="4" <?php if(trim($venuesRates[$i]['rating'])=="4") echo 'selected="selected"' ?>>Good</option>
                <option value="3" <?php if(trim($venuesRates[$i]['rating'])=="3") echo 'selected="selected"' ?>>Average</option>
                <option value="2" <?php if(trim($venuesRates[$i]['rating'])=="2") echo 'selected="selected"' ?>>Below Average</option>
                <option value="1" <?php if(trim($venuesRates[$i]['rating'])=="1") echo 'selected="selected"' ?>>Poor</option>
                </select>
                <?php $i++; endforeach; ?>
            </td>
        </tr>
-->
        
        <tr>
            <td><h2 align="center">Room Options</h2></td>
        </tr>
        
        <tr>
            <th>Place of Ceremony:</th>
            <th><?=$_POST['placeOfCeremony']?></th>
            <th><?=$_POST['guestsRoom']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['placeOfCeremony'],2)?></td>
            <?php endforeach; ?>
        </tr>
        
        <tr>
            <th>Day Reception:</th>
            <th><?=$_POST['dayReception']?></th>
            <th><?=$_POST['guestsRoom']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['dayReception'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Evening Reception:</th>
            <th><?=$_POST['eveningReception']?></th>
            <th><?=$_POST['guestsRoom']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['eveningReception'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <!--<tr>
            <th>Exclusivity Available:</th>
            <th><?=$_POST['exclusiveAvailable']?></th>
            <th>&nbsp;</th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['exclusiveAvailable'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        -->
        
        <tr>
            <th>Accommodation:</th>
            <th><?=$_POST['accommodation']?></th>
            <th><?=$_POST['guestsRoom']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['accommodation'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <td><h2 align="center">Wedding Breakfast</h2></td>
        </tr>
        
        <tr>
            <th>Arrival Drinks Packages:</th>
            <th><?=$_POST['arrivalDrinks']?></th>
            <th><?=$_POST['guestsBreakfast']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['arrivalDrinks'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Adult:</th>
            <th><?=$_POST['adultBreakfast']?></th>
            <th><?=$_POST['guestsBreakfast']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['adultBreakfast'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Child  (0-13):</th>
            <th><?=$_POST['child1Breakfast']?></th>
            <th><?=$_POST['guestsBreakfast']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['child1Breakfast'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Child  (14-17):</th>
            <th><?=$_POST['child2Breakfast']?></th>
            <th><?=$_POST['guestsBreakfast']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['child2Breakfast'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <th>Toast Drink:</th>
            <th><?=$_POST['toastDrink']?></th>
            <th><?=$_POST['guestsBreakfast']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['toastDrink'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        
        <tr>
            <td><h2 align="center">Evening Buffet</h2></td>
        </tr>
        
        <tr>
          <th>Adult:</th>
            <th><?=$_POST['adultBuffet']?></th>
            <th><?=$_POST['guestsBuffet']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['adultBuffet'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
          <th>Child  (0-13):</th>
            <th><?=$_POST['child1Buffet']?></th>
            <th><?=$_POST['guestsBuffet']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['child1Buffet'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
          <th>Child  (14-17):</th>
            <th><?=$_POST['child2Buffet']?></th>
            <th><?=$_POST['guestsBuffet']?></th>
        
            <?php foreach($venuesRates as $venuesRate): ?>
            <td>&pound;<?=number_format($venuesRate['child2Buffet'],2)?></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <td><h2 align="center">Discount Available</h2></td>
        </tr>
        
        <tr>
            <td colspan="3"><strong>Total</strong></td>
            <?php foreach($venuesRates as $venuesRate): ?>
            <td><strong>&pound;<?=number_format($venuesRate['total'],2)?></strong></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <td colspan="3"><strong>Discount</strong></td>
            <?php foreach($venuesRates as $venuesRate): ?>
            <td><strong><?=$venuesRate['discount']?>%</strong></td>
            <?php endforeach; ?>
            
        </tr>
        
        <tr>
            <td colspan="3"><strong>Gross Total</strong></td>
            <?php foreach($venuesRates as $venuesRate): ?>
            <td><strong>&pound;<?=number_format($venuesRate['gtotal'],2)?></strong></td>
            <?php endforeach; ?>
            
        </tr>
        
        </table>

        <div class="clear"></div>
    </section>
    <div class="clearfix"></div>
  </div>

<!-- Main Content Ends -->
</section>
<?php get_footer(); ?>